package com.zzyl.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.entity.User;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import com.zzyl.vo.AlertRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ Description:
 * @ BelongsProject: zzyl-cp
 * @ Author: G攀
 * @ CreateTime: 2024-09-14  16:41
 * @ Version: 1.0
 */
@Service
public class AlertDataServiceImpl implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 分页查询数据
     */
    @Override
    public ResponseResult<PageResponse<AlertDataVo>> getPage(AlertDataPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        // 获取 当前登陆者的ID
        Long userId = UserThreadLocal.getMgtUserId();
        // 正常获取告警数据
        Page<AlertDataVo> alertRuleVoList = alertDataMapper.findAllByCondition(dto, userId);

        // 封装返回的分页数据
        PageResponse<AlertDataVo> pageResponse = PageResponse.of(alertRuleVoList, AlertDataVo.class);
        return ResponseResult.success(pageResponse);
    }

    @Autowired
    private UserMapper userMapper;

    /**
     * 处理报警数据
     */
    @Override
    public void handleAlertData(Long alertDataId, AlertDataHandleDto dataHandleDto) {
        // 根据当前登陆者ID，获取当前登陆者信息
        Long userId = UserThreadLocal.getMgtUserId();
        User user = userMapper.selectByPrimaryKey(userId);
        // 获取当前登陆者的真实姓名
        String realName = user.getRealName();

        // 根据报警数据ID，找到报警规则ID
        AlertData dbAlertData = alertDataMapper.selectByPrimaryKey(alertDataId);

        // 填充报警数据信息
        dbAlertData.setProcessorId(userId);
        dbAlertData.setProcessorName(realName);
        dbAlertData.setProcessingResult(dataHandleDto.getProcessingResult());
        dbAlertData.setProcessingTime(dataHandleDto.getProcessingTime());
        dbAlertData.setStatus(1);
        // 更新报警信息
        alertDataMapper.updateByAlertRuleIdSelective(dbAlertData);
    }
}
